package com.wf.demo.info.web;

import com.wf.demo.info.dao.DeviceDAO;
import com.wf.demo.info.pojo.Repair;
import com.wf.demo.info.service.RepairService;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.info.util.Result;
import com.wf.demo.sys.annotation.SysLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RepairController {
    @Autowired
    RepairService repairService;
    @Autowired
    DeviceDAO deviceDAO;
    @GetMapping("/repairs")
    @RequiresPermissions("info:repair")
    public Page4Navigator<Repair> list(@RequestParam(value = "start",defaultValue = "0")int start, @RequestParam(value ="size",defaultValue = "15")int size)
            throws Exception {
        start = start<0? 0:start;
        Page4Navigator<Repair> page =repairService.list(start,size,5);
        return page;  //page类型为Page4Navigator,RestController会转换成json对象抛给浏览器
    }
    //单个设备的所有维修记录
    @GetMapping("/devices/{did}/repairs")
    @RequiresPermissions("info:repair")
    public Page4Navigator<Repair> list(@PathVariable("did")long did, @RequestParam(value = "start",defaultValue = "0")int start,
                                       @RequestParam(value = "size",defaultValue = "5")int size) throws Exception{
        start = start<0? 0:start;
        Page4Navigator<Repair> page = repairService.list(did,start,size,5);
        return page;
    }
    //
    @GetMapping("/repairs/{id}")
    @RequiresPermissions("info:repair")
    public Object get(@PathVariable("id")Long id){
       Repair bean= repairService.get(id);
        return bean;
    }

    @SysLog("保存维修信息")
   @PostMapping("/repairs")
   @RequiresPermissions("info:repair")
    public Object add(@RequestBody Repair bean) throws Exception{
        repairService.add(bean);
       return Result.addSuccess(bean);
    }

    @SysLog("删除维修信息")
    @DeleteMapping("/repairs/{id}")
    @RequiresPermissions("info:repair")
    public String delete(@PathVariable("id") Long id){
        repairService.delete(id);
        return null;
    }

    @SysLog("修改维修信息")
    @PutMapping("/repairs")
    @RequiresPermissions("info:repair")
    public Object update(@RequestBody Repair bean) throws Exception{
        repairService.update(bean);
        return Result.addSuccess(bean);
    }
    //根据控制系统编号搜索得到设备的维修记录
    @GetMapping("/devices/repairs")
    @RequiresPermissions("info:repair")
    public Page4Navigator<Repair> searchRepairByCsNumber(String str, @RequestParam(value = "start",defaultValue = "0")int start,
                                                  @RequestParam(value = "size",defaultValue = "15")int size) throws Exception{
        //根据控制系统编号搜索得到device ID
        long did =deviceDAO.findByCsNumber(str).getId();
        start = start<0? 0:start;
        Page4Navigator<Repair> page = repairService.list(did,start,size,5);
        return page;
    }

}
